﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using mars_server.csharp.model;
using MySql.Data.MySqlClient;
using mars_server.csharp.Util;
using mars_server.csharp.DB;

namespace mars_server.csharp.dao.impl
{
    public class RelationManagerImpl : RelationManager
    {

        private RelationManagerImpl() { }
        private static readonly RelationManagerImpl instance = new RelationManagerImpl();
        public static RelationManagerImpl Instance
        {
            get
            {
                return instance;
            }
        }

        string tableName = "mars_relation";

        ConnectionPool pool = ConnectionPool.getInstance();//mysql连接池，单例模式


        public RelationDO selectByPrimaryKey(long id)
        {
            throw new NotImplementedException();
        }


        /**
         * 根据用户Id来获取关系列表
         * */
        public List<RelationDO> selectRelationDOsByUserId(long userId)
        {
            List<RelationDO> RelationDOs = new List<RelationDO>();
            string query = SqlUtil.selectByIntQuery(tableName, "user_id", userId.ToString());

            Console.WriteLine(query);

            MySqlConnection connection = pool.getConnection();
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                MySqlDataReader dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    RelationDO relationDO = new RelationDO();
                    this.dataReaderToUserDO(relationDO, dataReader);
                    RelationDOs.Add(relationDO);
                }
                dataReader.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return RelationDOs;
        }


        public bool InsertRelationDO(RelationDO relationDO)
        {
            string query = "insert into mars_relation values (" + "null" +","+relationDO.userId +","+ relationDO.friendId + ")";
            Console.WriteLine(query);

            MySqlConnection connection = pool.getConnection();
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();

            }
            catch(Exception e)
            {
                Console.WriteLine(e.ToString());
                pool.releaseConnection(connection);
                return false;
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return true;
        }


        public bool deleteRelationDO(RelationDO relationDO)
        {
            MySqlConnection connection = pool.getConnection();
            string query = "delete from mars_relation where mars_relation.`user_id`=" + relationDO.userId + " and  mars_relation.`friend_id`=" + relationDO.friendId;
            Console.WriteLine(query);
            try
            {
                MySqlCommand cmd = new MySqlCommand(query, connection);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                pool.releaseConnection(connection);
                return false;
            }
            finally
            {
                pool.releaseConnection(connection);
            }
            return true;

        }
        //dataReader 转 Relation
        public void dataReaderToUserDO(RelationDO relationDO, MySqlDataReader dataReader)
        {
            relationDO.id = long.Parse(dataReader[0].ToString());
            relationDO.userId = long.Parse(dataReader[1].ToString());
            relationDO.friendId = long.Parse(dataReader[2].ToString());
        }

     


    }
}
